<?php

namespace DC\UserCenter\Models;

use App\Models\BaseModel;

class UserNodeModel extends BaseModel
{
    protected $table = 'users_node';

    protected $primaryKey = 'id';

    public $dateFormat = 'U';

    protected $guarded = [];

    /**
     * 添加用户
     * @param unknown $userinfo 用户信息
     * @param unknown $pinfo 父级节点信息
     */
    public function addnode($zhixiao_type,$userinfo,$pinfo){
        $data['user_id']=$userinfo->member_id;
        $data['x_pos']=$zhixiao_type*($pinfo->x_pos-1)+$pinfo->cnum+1;
        $data['y_pos']=$pinfo->y_pos+1;
        $data['pid']=$pinfo->id;
        $data['pids']=$pinfo->pids.$pinfo->id.',';
        $data['cnum']=0;
        $data['relat_pos']=$pinfo->cnum;
        $data['status']=1;
        $this->insert($data);
        //父级节点，下级人数加一
        $this->where(['id'=>$pinfo->id])->increment('cnum');
    }
    /**
     * 添加第一个节点
     * @param unknown $userinfo 用户信息
     * @param unknown $pinfo 父级节点信息
     * @param unknown $pos
     */
    public function addnodefirst($userinfo){
        $data['user_id']=$userinfo->member_id;
        $data['x_pos']=1;
        $data['y_pos']=0;
        $data['pid']=0;
        $data['pids']='0,';
        $data['cnum']=0;
        $data['relat_pos']=0;
        $data['status']=1;
        $this->insert($data);
    }
}
